﻿using System;
using System.Collections.Generic;
using System.Text;
using IService.Data.IManager;
using IService.DataDLL;
using System.Data;
using IService.Model;
using System.Data.OleDb;

namespace IService.Data.OlbImpl.ManagerImpl
{
	public class AdminDaManagerImpl : IAdminDaManager
	{
		public DTVAdmin GetUserById(DTVAdmin admin)
		{
			try
			{
				admin.Select();
				return admin;
			}
			catch (Exception e)
			{
				return admin;
			}
		}

		public DataTable GetUserListPagin(Type infoType, PagingEntity pe)
		{
			return DataAccess.SelectPaging(infoType, pe);
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public bool InsertUser(DTVAdmin admin)
		{
			return DataAccess.Insert(admin);
		}

		public bool UpdateUser(DTVAdmin admin)
		{
			return DataAccess.Update(admin);
		}

		public bool DeleteUser(DTVAdmin admin)
		{
			return DataAccess.Delete(admin);
		}

		public bool DeleteUserByIds(Type infoType, string WhereClause)
		{
			return DataAccess.Delete(infoType, WhereClause);
		}

		public DTVAdmin Login(string username, string userpass)
		{
			DTVAdmin currentuser = new DTVAdmin();
			string whereclause = "Username = '" + username + "' and Password='" + userpass + "'";
			if (currentuser.Select(whereclause))
			{
				return currentuser;
			}
			return null;
		}
		//通过参数方式登录，防止注入攻击
		public DTVAdmin LoginByParam(string username, string userpass)
		{
			DTVAdmin currentuser = new DTVAdmin();
			OleDbParameter parausername = new OleDbParameter("@Username", OleDbType.VarChar);
			parausername.Value = username;
			OleDbParameter parapassword = new OleDbParameter("@Password", OleDbType.VarChar);
			parapassword.Value = userpass;
			OleDbParameter[] parameters = new OleDbParameter[] { parausername, parapassword };
			string whereclause = "Username = @Username and Password=@Password";
			if (currentuser.Select(whereclause, parameters))
			{
				return currentuser;
			}
			return null;
		}
	}
}
